package com.nine.pesys.model.action;

import com.nine.pesys.base.BaseAction;
import com.nine.pesys.model.entity.*;
import com.nine.pesys.model.service.FieldInformationService;
import com.nine.pesys.util.annotation.Privilege;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * Created by L先森 on 2014/12/26.
 */
@Controller
@Scope("prototype")
@Namespace("/fieldImformation")
@ParentPackage("my_struts")
@Results({
        @Result(name = "fieldAppointment",location = "front/home.jsp"),
})
@Privilege
public class FieldImformationAction extends BaseAction<FileInformation>{

    private Long fieldDetailId;
    private String stuNumber;
    private Long[] ids;
    /**
     * 场地预约(只能本人在网上预约)
     * 需要信息 : FieldInformationId
     *                  stuNumber
     *                  lifeTime
     * @return
     * @throws Exception
     */
    @Action("/fieldAppointment")
    public String fieldAppointment() throws Exception{
        User user = (User) session.get("user");
//        预约后状态 场地剩余数量-1  已预约取真
//        选择的场地是否合法
        SportFieldDetail sportFieldDetail = this.sportFieldDetailService.getById(fieldDetailId);
        if(sportFieldDetail==null){
            ActionContext.getContext().put("message","请输入正确的场地号");
            return "error";
        }
//        将预约取真
        sportFieldDetail.setIsReserved(true);
        this.sportFieldDetailService.update(sportFieldDetail);
        SportField sportField = sportFieldDetail.getSportField();
        sportField.setLeftNum(sportField.getLeftNum()-1);
        this.sportFieldService.update(sportField);
//        保存information
        model.setSportFieldDetail(sportFieldDetail);
        model.setUser(user);
        this.fieldInformationService.save(model);
        return "fieldAppointment";
    }

    /**
     * 场地预约失效
     * 失效就不用给钱了
     * 需要的数据 FileInformation的id
     * @return
     * @throws Exception
     */
    @Privilege("manager")
    @Action("/releaseAppointment")
    public String releaseAppointment() throws Exception{
        fileInformations = this.fieldInformationService.findByIds(ids);
        for (FileInformation fileInformation:fileInformations){
            fileInformation.setIsReturn(true);
            this.fieldInformationService.update(fileInformation);
        }
        return "releaseAppointment";
    }

//    public String reserve

    public Long[] getIds() {
        return ids;
    }

    public void setIds(Long[] ids) {
        this.ids = ids;
    }

    public Long getFieldDetailId() {
        return fieldDetailId;
    }

    public void setFieldDetailId(Long fieldDetailId) {
        this.fieldDetailId = fieldDetailId;
    }

    public String getStuNumber() {
        return stuNumber;
    }

    public void setStuNumber(String stuNumber) {
        this.stuNumber = stuNumber;
    }
}
